//
//  NewsViewController.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/17.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "NewsViewController.h"
#import "JWnewsModel.h"
#import "JWNewsCell.h"
#import "JWNewsPictureModel.h"
#import "JWNewsPictureViewController.h"
#import "constant.h"
#import "RootBarViewController.h"
#import "JWNewsWebView.h"
#import "JWWebViewController.h"
#import "BaseNavigationController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface NewsViewController ()<UITableViewDataSource,UITableViewDelegate,UINavigationControllerDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) NSArray *newsAr;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *headerViewTitle;
@property (nonatomic, strong) NSArray *jsonAr;

@end

@implementation NewsViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"新闻";
        self.navigationController.navigationBar.translucent = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.delegate = self;
    
    self.jsonAr = [self objectWithJsonName:@"news_list"];//解析json数据
    
    [self loadTableView];
    [self tableViewHeaderWith:self.newsAr[0]];
    
}

- (NSArray *)newsAr{
    
    if (!_newsAr) {
        NSMutableArray *tempAr = [NSMutableArray array];
        
        for (int i = 0; i < _jsonAr.count; i++) {
            NSDictionary *movieInfoDic = _jsonAr[i];
            JWnewsModel *model = [JWnewsModel newsModelWithDictionary:movieInfoDic];
            
            [tempAr addObject:model];
            
        }
        _newsAr = tempAr;
    
    }
    
    return _newsAr;
    
}

- (void)loadTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, JWScreenWidth, JWScreenHeight - 64) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorColor = [UIColor blackColor];
    UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JWScreenWidth, 150)];
    _tableView.tableHeaderView = head;
    _tableView.tableHeaderView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.newsAr.count - 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JWNewsCell *cell = [JWNewsCell newsCellWithTableView:tableView];
    JWnewsModel *model = self.newsAr[indexPath.row + 1];
    cell.newsModel = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
}

#pragma mark - 表头设置

- (void)tableViewHeaderWith:(JWnewsModel *)model{
    
    UIView *hearderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JWScreenWidth, 150)];
    
    NSString *urlStr = model.image;//网址字符串
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];
    UIImage *image = [UIImage imageWithData:imageData];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:hearderView.frame];
    imageView.image = image;
    [hearderView addSubview:imageView];
    
    _headerViewTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, hearderView.frame.size.height - 30, hearderView.frame.size.width, 30)];
    _headerViewTitle.font = [UIFont systemFontOfSize:15];
    _headerViewTitle.textColor = [UIColor whiteColor];
    _headerViewTitle.text = model.title;
    _headerViewTitle.backgroundColor = [UIColor blackColor];
    _headerViewTitle.alpha = 0.5;
    _headerViewTitle.textAlignment = NSTextAlignmentCenter;
//    
    
    [self.view addSubview:hearderView];
    [self.view insertSubview:_headerViewTitle aboveSubview:hearderView];
    
    self.headerView = hearderView;
}

#pragma mark - 页面的跳转

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JWnewsModel *newsModel = self.newsAr[indexPath.row + 1];
    
    if (newsModel.type == 1) {//有图片，进入图片集
        
        UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc] init];

        JWNewsPictureViewController *newsPictureVC = [[JWNewsPictureViewController alloc] initWithCollectionViewLayout:flowLayout];
        
        newsPictureVC.jsonName = @"image_list";
        
        [self.navigationController pushViewController:newsPictureVC animated:YES];
        
    }
    
    if (newsModel.type == 0) {//    没有图，进入网页
        
        JWWebViewController *webVC = [[JWWebViewController alloc] init];
        BaseNavigationController *nvg = [[BaseNavigationController alloc] initWithRootViewController:webVC];
        [self presentViewController:nvg animated:YES completion:nil];
        
    }
    
    if (newsModel.type == 2) {//    有视屏
        
        //        视频播放
        
        // MPMoviePlayerViewController:视频播放控制器,需要导入<MediaPlayer/MediaPlayer.h>框架
        MPMoviePlayerViewController *ctrl = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:@"http://vf1.mtime.cn/Video/2012/06/21/mp4/120621104820876931.mp4"]];
        
        //将视频播放器弹出
        
        [self presentMoviePlayerViewControllerAnimated:ctrl];
   
    }
  
}

#pragma mark - tabbar的隐藏、

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    RootBarViewController *tabBC = (RootBarViewController *)self.navigationController.parentViewController;
    
    if (navigationController.viewControllers.count == 1) {
        tabBC.tabBar.hidden = NO;
    }else {
        tabBC.tabBar.hidden = YES;
    }
    
}

#pragma mark - 设置表头的变化

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat off = self.tableView.contentOffset.y;
    
    self.headerViewTitle.transform = CGAffineTransformMakeTranslation(0, -off);
    
    if (off < 0) {
        CGFloat scale = (-off + 150) / 150.f;
        self.headerView.transform = CGAffineTransformMakeScale(scale, scale);   //  拉伸放大
        
        CGFloat newH = scale * 150;
        CGFloat newW = scale * JWScreenWidth;
        CGRect frame = CGRectMake(-(scale - 1) * JWScreenWidth / 2, 0, newW, newH);
        self.headerView.frame = frame;
        
    }else {
        self.headerView.transform = CGAffineTransformMakeTranslation(0, -off);      //上推上移
    }
    
}





















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
